Construct a cross-report

Now let us turn from theory to practice. Let us construct a simple cross-report, which would display employees’ salary during four years. To perform this, we need the "simplecross.db" table, which is available in the FastReport "DEMOS\CROSS\DATA" folder. The table contains data of the following kind:

Name Year Salary
Ann 1999 3300
Ben 2002 2000
….

As usually, let us create a new project in Delphi, put the "TTable," "TfrxDBDataSet," and "TfrxReport" components on the form and set them:

Table1:
DatabaseName = 'c:\Program Files\FastReport3\Demos\Cross\Data'
TableName = ' simplecross.db'

the DatabaseName property value of course must correspond with the path to your folder with FastReport!

frxDBDataSet1:
DataSet = Table1
UserName = 'SimpleCross'

For cross-reports construction, one should use the "TfrxCrossObject" component  from the FastReport component palette. Just put it on the form; it is not required to set anything. At the same time, the "frxCross" unit, which contains all necessary functionality, will be added to the "uses" list.

Let us enter the report designer. First of all, connect our data source to the "Report|Data…"menu. Put the "DB cross-tab" object on the report list:

On the designer list the object looks lowly:

All settings are specified with the help of the object editor. Let us call it by double-clicking on the object:

The following items are denoted by figures in the picture:

1 – the drop-down list with available data sources;
2 – the list of fields in the selected data source. The fields from this list can be dragged to the "4," "5," and "6" lists;
3 – here one can specify whether it is necessary to display titles and totals;
4 – the list of fields, which generate a line title;
5 – the list of fields, which generate a column title;
6 – the list of fields, which generate a table cell;
7 – here the future table structure is previewed. All the elements in the table are clickable;
8 – toolbar for modifying table design:

 - table style select;

 - cell font parameters;

 - text alignment;

 - text rotation;

 - conditional highlighting;

 - cell format;

 - cell frame and filling.

As you can see, it is possible to operate here only with the help of the mouse. In our case, it is enough to drag fields from the "2" list to the "4," "5," and "6" lists, as it is shown in the picture. Let us not do anything yet. Close the editor by clicking the "ÎÊ" button  . If starting the report now, you would see a table like the one below:

Well, it is exactly what we wanted to receive. Let us continue examining the object. Call the object editor once again. The first thing we want to perform is to modify the titles’ colors and to display "Total" instead of "Grand total." It is very easy to perform when using the bottom editor field (N7 in the picture). Here the cross-table structure is displayed, and it can also be set with the help of the mouse. The active call is displayed with an orange frame:

To change the title color into gray, click on the "Year," "Name," and "Grand Total" objects one after another, and then select the desired color via the  button in the toolbar. To change the "Grand Total" inscription, double-click on the cell, and then you will see the familiar text editor, where one should type "Total." After that, our report will look as follows:

It remains to set a format, where the currency values are displayed. To perform this, in the cross-object editor, click on the "Total" object and the object, representing a cell (with the "0" text) one after another and select the required format by clicking on the  button in the toolbar. You will get the following result: